package com.code9xs.controller;

import com.alibaba.fastjson.JSONObject;
import com.code9xs.pojo.Article;
import com.code9xs.pojo.LikeAndFavorites;
import com.code9xs.util.DateUtils;
import com.code9xs.util.PageUtil;
import com.code9xs.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Suhd
 * @date 2021/5/11
 * @time 10:37
 * @wechat i9xswanan
 * @github https://github.com/Code9xs
 */
@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController{
    @PostMapping("/publisher")
    public Result publishArticle(@RequestBody JSONObject param){
        Article article = new Article();
        String title = param.getString("title");
        String content = param.getString("content");
        article.setTitle(title);
        article.setText(content);
        article.setStatus("已发布");
        article.setDeletion(false);
        article.setTime(DateUtils.getCurrentTime());
        article.setUid(userService.getCurrentUser().getId());
        boolean flag = articleService.publishArticle(article);
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }

    @PostMapping("/save")
    public Result saveArticle(@RequestBody JSONObject param){
        Article article = new Article();
        String title = param.getString("title");
        String content = param.getString("content");
        article.setTitle(title);
        article.setText(content);
        article.setStatus("暂存");
        article.setDeletion(false);
        article.setTime(DateUtils.getCurrentTime());
        article.setUid(userService.getCurrentUser().getId());
        boolean flag = articleService.publishArticle(article);
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }

    @GetMapping("/all")
    public Result getAllArticlePagination(@RequestParam String page,@RequestParam String pageSize){
        PageUtil data = articleService.getAllArticlePagination(Integer.valueOf(page), Integer.valueOf(pageSize));
        return Result.succ(HttpServletResponse.SC_OK,"成功",data);
    }

    @GetMapping("/userBar")
    public Result getUserInfoBar(){
        return Result.succ(HttpServletResponse.SC_OK,"成功",articleService.getUserInfoBar());
    }

    @GetMapping("/comment")
    public Result getArticleCommentByArticleId(@RequestParam("articleId")String articleId){
        return articleService.getArticleCommentByArticleId(Integer.valueOf(articleId));
    }

    @PostMapping("/like")
    public Result clickLike(@RequestBody JSONObject param){
        final boolean flag = articleService.clickLike(param.getInteger("articleId"));
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }

    @PostMapping("/dislike")
    public Result cancelLike(@RequestBody JSONObject param){
        articleService.dislike(param.getInteger("articleId"));
        return Result.succ(HttpServletResponse.SC_OK,"成功",null);
    }

    @PostMapping("/favorites")
    public Result clickFavorites(@RequestBody JSONObject param){
        final boolean flag = articleService.clickFavorites(param.getInteger("articleId"));
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }

    @PostMapping("/cancelFavorite")
    public Result cancelFavorite(@RequestBody JSONObject param){
        articleService.cancelFavorite(param.getInteger("articleId"));
        return Result.succ(HttpServletResponse.SC_OK,"成功",null);
    }

    @GetMapping("/isLikeOrFavorites")
    public Result getUserIsLikeOrFavorites(@RequestParam("articleId")String articleId){
        LikeAndFavorites userIsLikeOrFavorites = articleService.getUserIsLikeOrFavorites(Integer.valueOf(articleId));
        return Result.succ(HttpServletResponse.SC_OK,"成功",userIsLikeOrFavorites);
    }

    @PostMapping("/views")
    public Result addViews(@RequestBody JSONObject param){
        boolean flag = articleService.addViews(param.getInteger("articleId"));
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }

    @GetMapping("/item")
    public Result getArticleItem(@RequestParam("articleId")String articleId){
        return Result.succ(HttpServletResponse.SC_OK,"成功",articleService.getArticleItem(Integer.valueOf(articleId)));
    }

    @PostMapping("/publishComment")
    public Result publishComment(@RequestBody JSONObject param){
        boolean flag = articleService.publishComment(param.getInteger("articleId"), param.getString("content"));
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }

    @PostMapping("publishUserComment")
    public Result publishUserComment(@RequestBody JSONObject param){
        boolean flag = articleService.publishUserComment(param.getInteger("articleId"), param.getInteger("replyCommentId"), param.getString("content"));
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }

    @GetMapping("/mine")
    public Result getMyArticle(@RequestParam("page")String page,@RequestParam("pageSize")String pageSize){
        PageUtil data = articleService.getCurrentUserArticle(Integer.valueOf(page), Integer.valueOf(pageSize));
        return Result.succ(HttpServletResponse.SC_OK,"成功",data);
    }

    @PostMapping("/update/item")
    public Result updateUserArticle(@RequestBody JSONObject param){
        Integer articleId = param.getInteger("articleId");
        String content = param.getString("content");
        String title = param.getString("title");
        boolean flag = articleService.updateArticle(articleId, content, title);
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }

    @PostMapping("/updateSave/item")
    public Result updateSaveUserArticle(@RequestBody JSONObject param){
        Integer articleId = param.getInteger("articleId");
        String content = param.getString("content");
        String title = param.getString("title");
        boolean flag = articleService.updateSaveUserArticle(articleId, content, title);
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }

    @PostMapping("/deletion")
    public Result deleteArticle(@RequestBody JSONObject param){
        final Integer articleId = param.getInteger("articleId");
        final boolean flag = articleService.deleteArticle(articleId);
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }

    @GetMapping("/index/article/max")
    public Result getMaxArticle(){

        return Result.succ(HttpServletResponse.SC_OK,"成功",articleService.getMaxArticle());
    }



}
